home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Stopwatch2.3 / Source / StopWatch.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  97 lines

  1. /*
  2.  * Stopwatch/time support for Stopwatch app.
  3.  * Author: Rich Plevin
  4.  *
  5.  * For legal stuff see the file COPYRIGHT
  6.  */
  7. #import "StopWatch.h"
  8.  
  9. @implementation StopWatch
  10.  
  11. - startWatch
  12. {
  13.   if ( running == YES )
  14.     return nil;
  15.  
  16.   gettimeofday( &start, NULL );
  17.   running = YES;
  18.   return self;
  19. }
  20.  
  21. - stopWatch
  22. {
  23.   if ( running == NO )
  24.     return nil;
  25.  
  26.   /* Note: Second arg (timezone) is ignored on NeXT */
  27.   gettimeofday( &finish, NULL );
  28.   running = NO;
  29.   return self;
  30. }
  31.  
  32. - (BOOL) running
  33. {
  34.   return running;
  35. }
  36.  
  37. /*
  38.  * If its running, show time from start until now, otherwise show
  39.  * the time that passed between the saved start and finish.
  40.  */
  41. - (int) elapsedMinutes
  42. {
  43.   int mins, secs;
  44.   struct timeval end;
  45.   
  46.   if ( running == YES )
  47.     gettimeofday( &end, NULL );
  48.   else
  49.     end = finish;
  50.  
  51.   secs = end.tv_sec - start.tv_sec;
  52.   mins = secs / 60;
  53.   if ( secs % 60 >= 30 )
  54.     ++mins;            /* round up if necessary */
  55.  
  56.   return mins;
  57. }
  58.  
  59. - (const char *) elapsedTime
  60. {
  61.   int hrs, mins;
  62.   static char buf[6];    /* enough for a "hh:mm" string - a 99 hour binge... */
  63.   
  64.   mins = [self elapsedMinutes];
  65.  
  66.   hrs  = mins / 60;        /* total number of hours */
  67.   mins %= 60;            /* remainder number of minutes */
  68.  
  69.   /*
  70.    * NB: Make the buffer larger if the format string requires it!!
  71.    */
  72.   sprintf( buf, "%02d:%02d", hrs, mins );
  73.   return (const char *)buf;
  74. }
  75.  
  76. - (const char *)startTimeString
  77. {
  78.   static char buf[6];    /* enough for a "hh:mm" string */
  79.   struct tm *tm;
  80.  
  81.   tm = localtime(&start.tv_sec);
  82.   sprintf( buf, "%02d:%02d", tm->tm_hour, tm->tm_min );
  83.   return (const char *)buf;
  84. }
  85.  
  86. - (const char *)startDateString;
  87. {
  88.   static char buf[9];    /* enough for a "mm/dd/yy" string */
  89.   struct tm *tm;
  90.  
  91.   tm = localtime(&start.tv_sec);
  92.   sprintf( buf, "%02d/%02d/%02d", tm->tm_mon + 1, tm->tm_mday, tm->tm_year );
  93.   return (const char *)buf;
  94. }
  95.  
  96. @end
  97.